<?php

/**
 * This is the model class for table "tbl_diretorio".
 *
 * The followings are the available columns in table 'tbl_diretorio':
 * @property string $id
 * @property string $parent_id
 * @property string $diretorio
 * @property string $assunto
 * @property integer $id_area
 * @property integer $id_gestor
 * @property string $descricao
 * @property integer $gr_global
 * @property integer $gr_leitura
 * @property integer $gr_gravacao
 */
class Diretorio extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_diretorio';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('diretorio', 'required'),
			array('id, id_area, id_gestor, gr_global, gr_leitura, gr_gravacao', 'numerical', 'integerOnly'=>true),
			array('parent_id', 'length', 'max'=>10),
			array('diretorio', 'length', 'max'=>50),
			array('assunto', 'length', 'max'=>60),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, parent_id, diretorio, assunto, id_area, id_gestor, descricao, gr_global, gr_leitura, gr_gravacao', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'parent_id' => 'Parent',
			'diretorio' => 'Diretorio',
			'assunto' => 'Assunto',
			'id_area' => 'Id Area',
			'id_gestor' => 'Id Gestor',
			'descricao' => 'Descricao',
			'gr_global' => 'Gr Global',
			'gr_leitura' => 'Gr Leitura',
			'gr_gravacao' => 'Gr Gravacao',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('parent_id',$this->parent_id,true);
		$criteria->compare('diretorio',$this->diretorio,true);
		$criteria->compare('assunto',$this->assunto,true);
		$criteria->compare('id_area',$this->id_area);
		$criteria->compare('id_gestor',$this->id_gestor);
		$criteria->compare('descricao',$this->descricao,true);
		$criteria->compare('gr_global',$this->gr_global);
		$criteria->compare('gr_leitura',$this->gr_leitura);
		$criteria->compare('gr_gravacao',$this->gr_gravacao);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
                        'pagination' => array('pageSize' => 300,),
		));
	}

        
        public function getDiretorioOptions()
        {
            return CHtml::listData(Diretorio::model()->findAll(),'id','diretorio');
        }       
       public function getPathOptions()
        { //Retorna a relação de diretórios ordenados alfabeticamete
            $model = Diretorio::model()->findAll();
            
            foreach ($model as $key){
                    $Saida=$key->id;
                    $arrayPath[$Saida]=Diretorio::model()->getPath($key->parent_id, $key->diretorio);
                } 
            asort($arrayPath);
            return $arrayPath;
        }  
        
        public function getPath($pathId,$arraySaida) 
        { //Retorna o caminho completo baseado no ID do Filho
                $criteria = new CDbCriteria;
                $criteria->compare(id,$pathId);
                $model = Diretorio::model()->findAll($criteria);

                foreach ($model as $key) {
                    $saida=$key->diretorio."/".$arraySaida;    
                    $arraySaida=Diretorio::model()->getPath($key->parent_id, $saida);
                }   
               return $arraySaida;
        }
       //http://stackoverflow.com/questions/11483549/get-category-tree-recursively-yii 
        
        
        public function getChildren($path, $parent, $level=0) { 
                $criteria = new CDbCriteria;
                $criteria->condition='parent_id=:id';
                $criteria->params=array(':id'=>$parent);
                $model = Diretorio::model()->findAll($criteria);
                              
                foreach ($model as $key) {
                    $arraySaida[$key->id] .= $path ."/".  $key->diretorio;
                    $outId .= $key->diretorio;
                    Diretorio::model()->getChildren($outId, $key->id, $level+1);
                    
                    //return $arraySaida;
                }   
                //print_r($arraySaida);
               return $arraySaida;
            }
            
        public function getChildrenString($parent) { 
                $criteria = new CDbCriteria;
                $criteria->condition='parent_id=:id';
                $criteria->params=array(':id'=>$parent);
                $model = Diretorio::model()->findAll($criteria);
                foreach ($model as $key) {
                    $storage = '';
                    $storage .=$key->id . ','; 
                   echo $storage .= Diretorio::model()->getChildrenString($key->id);
                }
                $storage;              
            }
                   
        
	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Diretorio the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
